import React, { Component } from "react";
import { Select, Modal, Form, Input, Button, message } from "antd";
import EditableTable from "./EditTable";
// import { accountCate } from "../../config/enums";
const { Option } = Select;

export default class Address extends Component {
  state = {
    submitLoading: false,
    formData: [],
    selectOption: null,
    formKey: [
      { accountCate: 1, name: "USDT" },
      { accountCate: 3, name: "FIL" },
    ],
    showModal: false,
  };
  getData = async (e) => {
    const res = await this.http("Receipt", {
      accountCate: e,
      isCompany: true,
    });
    this.setState({
      formData: res.data.dtos,
    });
  };
  onFinish = async (values) => {
    this.setState({ submitLoading: true });
    values.accountCate = this.state.selectOption;
    values.isCompany = true;
    // console.log(values);
    // return;
    await this.http("Receipt", values, "POST");
    this.setState({ submitLoading: false, showModal: false });
    this.getData(this.state.selectOption);
  };
  handleChange = (e) => {
    console.log(e);
    this.setState({
      selectOption: e,
    });
    this.getData(e);
  };
  addClick = () => {
    if (this.state.selectOption === null) {
      message.error("请选择币种");
      return;
    }
    this.setState({
      showModal: true,
    });
  };
  componentDidMount() {
    // this.getData();
  }
  render() {
    return (
      <div>
        <div style={{ margin: "20px" }}>
          <Modal
            title="创建配置"
            footer={[null, null]}
            onCancel={() => {
              this.setState({
                showModal: false,
              });
            }}
            visible={this.state.showModal}
          >
            <Form name="basic" onFinish={this.onFinish}>
              <Form.Item
                label="地址名称"
                name="pathName"
                rules={[
                  {
                    required: true,
                    message: "地址名称不可为空！",
                  },
                ]}
              >
                <Input />
              </Form.Item>
              <Form.Item
                label="地址值"
                name="info"
                rules={[
                  {
                    required: true,
                    message: "地址值不可为空！",
                  },
                ]}
              >
                <Input />
              </Form.Item>
              <Form.Item>
                <Button
                  loading={this.state.submitLoading}
                  type="primary"
                  htmlType="submit"
                >
                  提交
                </Button>
              </Form.Item>
            </Form>
          </Modal>
          选择币种：
          <Select
            value={this.state.selectOption}
            style={{ width: 150 }}
            onChange={this.handleChange}
          >
            {this.state.formKey.map((item, i) => {
              return (
                <Option key={item.accountCate} value={item.accountCate}>
                  {item.name}
                </Option>
              );
            })}
          </Select>
          <Button
            type="primary"
            style={{
              marginLeft: "20px",
            }}
            onClick={this.addClick}
          >
            新增
          </Button>
        </div>

        <EditableTable refresh={this.getData} formData={this.state.formData} />
      </div>
    );
  }
}
